/* $$Id: RssFeedList.java 30 2008-01-09 15:16:48Z qxodream $$
 *
 * RssReaderMidlet --  RSS reader for J2ME(midp)
 * The original codes of this porgram is from
 * RssOwlMIDlet(zhengyun@gmail.com)
 *
 * Copyright (c) 2007-2008 QXO(qxodream@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.ultrapower.view;

import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;

import com.ultrapower.control.GUIController;
import com.ultrapower.model.RssFeed;
import com.ultrapower.model.RssItem;
import com.ultrapower.tools.ResUtils;

/**
 * 类说明：显示当前所有的Rss Feed书签,以名称列表方式显示。 <br/> 提供菜单命令有： 读取新闻 添加书签 编辑书签 删除书签 关于我
 *
 * @author zhengyun@ultrapower 2005.10.07
 * @author QXO(qxodream@gmail.com)
 *
 */
public class RssFeedList extends javax.microedition.lcdui.List {
	private GUIController controller;
	private Command EXIT_COMMAND;
	private Command ADD_COMMAND;
	private Command EDIT_COMMAND;
	private Command VIEW_COMMAND;
	private Command DELETE_COMMAND;
	private Command ABOUT_COMMAND;
	// private Command commandOK;

	private Vector items;
	private static Image iconFeed = ResUtils
			.getImage(ResUtils.IMG_FEEDLIST);

	// contructor
	public RssFeedList(String title, GUIController control, Vector vec) {
		super(title, List.IMPLICIT);

		controller = control;
		this.setCommandListener(new RssFeedListListener());
		this.items = vec;

		EXIT_COMMAND = new Command(ResUtils
				.getLabel(ResUtils.CMD_QUIT), Command.EXIT, 1);
		VIEW_COMMAND = new Command(ResUtils
				.getLabel(ResUtils.KEY_READ_FEED), Command.ITEM, 2);
		ADD_COMMAND = new Command(ResUtils
				.getLabel(ResUtils.KEY_ADD_FEED), Command.ITEM, 2);
		EDIT_COMMAND = new Command(ResUtils
				.getLabel(ResUtils.KEY_EDIT_FEED), Command.ITEM, 2);
		DELETE_COMMAND = new Command(ResUtils
				.getLabel(ResUtils.KEY_DEL_FEED), Command.ITEM,
				2);
		ABOUT_COMMAND = new Command(ResUtils
				.getLabel(ResUtils.KEY_ABOUTME), Command.ITEM, 2);

		this.addCommand(EXIT_COMMAND);
		this.addCommand(VIEW_COMMAND);
		this.addCommand(ADD_COMMAND);
		this.addCommand(EDIT_COMMAND);
		this.addCommand(DELETE_COMMAND);
		this.addCommand(ABOUT_COMMAND);

		/*
		 * this.append( String.valueOf(SResources.getChars(
		 * SResources.TXT_T_MENU_COMMAND_READRSSFEED)) , iconFeed); this.append(
		 * String.valueOf(SResources.getChars(
		 * SResources.TXT_T_MENU_COMMAND_ADDRSSFEED)) , iconFeed); this.append(
		 * String.valueOf(SResources.getChars(
		 * SResources.TXT_T_MENU_COMMAND_EDITRSSFEED)) , iconFeed); this.append(
		 * String.valueOf(SResources.getChars(
		 * SResources.TXT_T_MENU_COMMAND_DELETERSSFEED)) , iconFeed);
		 * this.append( String.valueOf(SResources.getChars(
		 * SResources.TXT_T_MENU_COMMAND_ABOUTME)) , iconFeed); this.append(
		 * String.valueOf(SResources.getChars(
		 * SResources.TXT_T_MENU_COMMAND_QUIT)) , iconFeed);
		 */

		if (vec != null && vec.size() > 0) {
			RssFeed item = null;
			for (int i = 0; i < vec.size(); i++) {
				item = (RssFeed) vec.elementAt(i);
				this.append(item.getName(), iconFeed);
			}
		}

	}

	public void refresh(Vector vec) {
		this.deleteAll();
		this.items = vec;
		if (vec != null && vec.size() > 0) {
			RssFeed item = null;
			for (int i = 0; i < vec.size(); i++) {
				item = (RssFeed) vec.elementAt(i);
				this.append("  " + item.getName(), iconFeed);
			}
		}
	}

	public void add(RssItem item) {
		this.append(item.getTitle(), iconFeed);
	}

	/*
	 * 由于MIDP 2.0中才定义了List的deleteAll()方法, 但是MIDP1.0中List支持delete(int itemNum).
	 * 所以在MIDP1.0中,可以使用List的size()得到当前List中的Item的数量, 然后使用for循环delete(int
	 * itemNum),这样在MIDP1.0中实现deleteAll的功能。
	 */
	public synchronized void deleteAll() {
		System.out.println("!-- Enter RssFeedList::deleteAll --!");
		for (int i = 0; i < this.size(); i++) {
			this.delete(i);
		}
	}

	/*
	 * 内部监听器，监听器监听所有Command事件，并把事件响应推出来让控制器处理
	 */
	private class RssFeedListListener implements CommandListener {
		public void commandAction(Command command, Displayable disp) {
			if (command == EXIT_COMMAND) {
				controller.handleEvent(GUIController.EventID.EVENT_EXIT, null);
			} else if (command == VIEW_COMMAND
					|| (command == List.SELECT_COMMAND)) {
				int ind = ((List) disp).getSelectedIndex();
				if (ind == -1)
					return;
				Object[] args = { items.elementAt(ind) };
				controller.handleEvent(GUIController.EventID.EVENT_VIEW_DETAIL,
						args);
			} else if (command == ADD_COMMAND) {
				controller.handleEvent(GUIController.EventID.EVENT_NEW_RECORD,
						null);
			} else if (command == EDIT_COMMAND) {
				int ind = ((List) disp).getSelectedIndex();
				if (ind == -1)
					return;
				Object[] args = { items.elementAt(ind) };
				controller.handleEvent(GUIController.EventID.EVENT_EDIT, args);
			} else if (command == DELETE_COMMAND) {
				int ind = ((List) disp).getSelectedIndex();
				if (ind == -1)
					return;
				Object[] args = { items.elementAt(ind) };
				controller
						.handleEvent(GUIController.EventID.EVENT_DELETE, args);
			} else if (command == ABOUT_COMMAND) {
				controller.handleEvent(GUIController.EventID.EVENT_ABOUT, null);
			}
		}
	}
}
